//
//  UIColor+XUtilExtension.m
//  XUtils
//
//  Created by Hongwei Wang on 2018/8/6.
//  Copyright © 2018年 Hongwei Wang. All rights reserved.
//

#import "XUtilsMacros.h"
#import "NSString+XUtilExtension.h"
#import "UIColor+XUtilExtension.h"

NSInteger int_duplicate4bits(NSInteger value) {
    return (value << 4) + value;
}

@implementation UIColor (XUtilExtension)

+ (instancetype)HexA6ColorHexValue:(unsigned long long)hex6 alpha:(CGFloat)alpha {
    NSInteger red = (hex6 & 0xff0000) >> 16;
    NSInteger green = (hex6 & 0xff00) >> 8;
    NSInteger blur = (hex6 & 0xff);
    return [UIColor RGBAColorRed:red blue:blur green:green alpha:alpha];
}

+ (instancetype)Hex6ColorHexValue:(unsigned long long)hex6 {
    return [self HexA6ColorHexValue:hex6 alpha:1];
}

+ (instancetype)HexA3ColorHexvalue:(NSUInteger)hex3 alpha:(CGFloat)alpha {
    CGFloat red = int_duplicate4bits((hex3 & 0xf00) >> 8);
    CGFloat green = int_duplicate4bits((hex3 & 0xf0) >> 4);
    CGFloat blur = int_duplicate4bits((hex3 & 0xf));
    return [UIColor RGBAColorRed:red blue:blur green:green alpha:alpha];
}

+ (instancetype)Hex3ColorHexvalue:(NSUInteger)hex3 {
    return [self HexA3ColorHexvalue:hex3 alpha:1];
}

+ (instancetype)HexAColorWithString:(NSString *)hexStr alpha:(CGFloat)alpha {
    hexStr = [[hexStr uppercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([hexStr hasPrefix:@"#"]) {
        hexStr = [hexStr substringFromIndex:1];
    } else if ([hexStr hasPrefix:@"0X"]) {
        hexStr = [hexStr substringFromIndex:2];
    }
    BOOL isNumber = [hexStr checkStringWithRegex:@"[0-9]*"];
    if (!isNumber) {
        return nil;
    }
    NSScanner *scanner = [NSScanner scannerWithString:hexStr];
    scanner.scanLocation = 0;
    unsigned long long hexValue;
    [scanner scanHexLongLong:&hexValue];
    UIColor *res = nil;
    switch (hexStr.length) {
        case 3:
            res = [UIColor HexA3ColorHexvalue:hexValue alpha:alpha];
            break;
        case 6:
            res = [UIColor HexA6ColorHexValue:hexValue alpha:alpha];
            break;
        default:
            break;
    }
    return res;
}

+ (instancetype)HexColorWithString:(NSString *)hexStr {
    return [self HexAColorWithString:hexStr alpha:1];
}

+ (instancetype)RGBAColorRed:(CGFloat)red blue:(CGFloat)blue green:(CGFloat)green alpha:(CGFloat)alpha {
    if (alpha > 1) {
        alpha = 1;
    } else if (alpha < 0) alpha = 0;
    return [UIColor colorWithRed:red/255. green:green/255. blue:blue/255. alpha:alpha];
}

+ (instancetype)RGBColorRed:(CGFloat)red blue:(CGFloat)blue green:(CGFloat)green {
    return [self RGBAColorRed:red blue:blue green:green alpha:1];
}


+ (instancetype)mixColor1:(UIColor*)color1 color2:(UIColor *)color2 ratio:(CGFloat)ratio {
    if(ratio > 1)
        ratio = 1;
    const CGFloat * components1 = CGColorGetComponents(color1.CGColor);
    const CGFloat * components2 = CGColorGetComponents(color2.CGColor);
//    NSLog(@"Red1: %f", components1[0]);
//    NSLog(@"Green1: %f", components1[1]);
//    NSLog(@"Blue1: %f", components1[2]);
//    NSLog(@"Red2: %f", components2[0]);
//    NSLog(@"Green2: %f", components2[1]);
//    NSLog(@"Blue2: %f", components2[2]);
 
    NSLog(@"ratio = %f",ratio);
    CGFloat r = components1[0]*ratio + components2[0]*(1-ratio);
    CGFloat g = components1[1]*ratio + components2[1]*(1-ratio);
    CGFloat b = components1[2]*ratio + components2[2]*(1-ratio);
//    CGFloat alpha = components1[3]*ratio + components2[3]*(1-ratio);
    
    return [UIColor colorWithRed:r green:g blue:b alpha:1];
}

+ (instancetype)RandomColor {
    return [UIColor colorWithRed:arc4random_uniform(255)/255. green:arc4random_uniform(255)/255. blue:arc4random_uniform(255)/255. alpha:1];
}

+ (instancetype)NRWhithColor {
    return [self Hex6ColorHexValue:0xeeeeee];
}

+ (instancetype)NRRedColor {
    return [self Hex6ColorHexValue:0xe55851];
}

+ (instancetype)NRBlackColor {
    return [self Hex6ColorHexValue:0x333333];
}

+ (instancetype)NRLightTextColor {
    return [self Hex6ColorHexValue:0x888888];
}
+ (instancetype)NRLightGrayColor {
    return [self Hex6ColorHexValue:0x999999];
}

+ (instancetype)NRCyanColor {
    return [UIColor Hex6ColorHexValue:0x00A0FF];
}
@end
